﻿<?php
require_once "config.php";
require_once "OpenApiV3.php";
require_once "server.class.php";
require_once "db.class.php";

$userId = $_POST['userId'];

// 用户的OpenID/OpenKey
$openid = $_POST['openid'];
$openkey = $_POST['openkey'];
$pf = $_POST['pf'];


$sdk = new OpenApiV3($appid, $appkey);
$sdk->setServerName($server_name);

$qqRet = get_friends_info($sdk, $openid, $openkey, $pf);

$xmlContent = "";

if ($qqRet['ret'] == 0)
{
	$friendItems = $qqRet['items'];
	$fopenids = $friendItems[0]['openid'];
	for ($i = 1; $i < min(count($friendItems), 100); $i++)
	{
		$fopenids .= "_".$friendItems[$i]['openid'];
	}
	$qqRet = get_multi_info($sdk, $openid, $openkey, $pf, $fopenids);
	if ($qqRet['ret'] == 0)
	{
		$friendItems = $qqRet['items'];
		$params = array("host"=>"$host", "user"=>"$user", "pass"=>"$pass", "db"=>"$db");
		$db = new Dbclass($params, false);
		$server = new ServerHelperClass($db, $tables);
		$sqlTail = " (openId = '".$friendItems[0]['openid']."'";
		for ($i = 1; $i < count($friendItems); $i++)
		{
			$sqlTail .= " or openId = '".$friendItems[$i]['openid']."'";
		}
		$sqlTail .= ")";
		//获取好友社交信息
		$sql = "select openId, count(*) as attackedCount from ".$tb_user_profile." a inner join ".$tb_social_profile." b on a.id = b.targetId where dayofyear(attackDate) = dayofyear(CURRENT_TIMESTAMP) and ".$sqlTail." group by openId";
		$result = $db->query($sql);
		$attackedCount = array();
		while ($arr = $db->fetchArray($result))
		{
			$attackedCount[$arr['openId']] = $arr['attackedCount'];
		}
		//获取好友信息
		$sql = "select id as playerId, campTankId, openId from ".$tb_user_profile." where ".$sqlTail;
		$result = $db->query($sql);
		$playerIds = array();
		while ($arr = $db->fetchArray($result))
		{
			$playerIds[$arr['openId']] = $arr['playerId'];
		}
		for ($i = 0; $i < count($friendItems); $i++)
		{
			$openId =  $friendItems[$i]['openid'];
			if (isset($playerIds[$openId]))
			{
				$xmlContent .= "<playerInfo>\n";
				$xmlContent .= "<playerId><![CDATA[".$playerIds[$openId]."]]></playerId>\n";
				$xmlContent .= "<openId><![CDATA[".$openId."]]></openId>\n";
				$xmlContent .= "<nickname><![CDATA[".$friendItems[$i]['nickname']."]]></nickname>\n";
				$xmlContent .= "<figureurl><![CDATA[".$friendItems[$i]['figureurl']."]]></figureurl>\n";
				$xmlContent .= "<is_yellow_vip><![CDATA[".$friendItems[$i]['is_yellow_vip']."]]></is_yellow_vip>\n";
				$xmlContent .= "<is_yellow_year_vip><![CDATA[".$friendItems[$i]['is_yellow_year_vip']."]]></is_yellow_year_vip>\n";
				$xmlContent .= "<yellow_vip_level><![CDATA[".$friendItems[$i]['yellow_vip_level']."]]></yellow_vip_level>\n";
				$xmlContent .= "<attackedCount><![CDATA[".$attackedCount[$openId]."]]></attackedCount>\n";
				$xmlContent .= "</playerInfo>\n";
			}
		}
		$db->close();
		$return = "success";
	}
	else
	{
		$return = "fail";
	}
}
else
{
	$return = "fail";
}

$txt = "";
$txt .= "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$txt .= "<values>\n";
$txt .= "<debug><![CDATA[".$debug."]]></debug>\n";
$txt .= "<action><![CDATA[getFriendList]]></action>\n";
$txt .= "<result><![CDATA[".$return."]]></result>\n";
$txt .= "<timeStamp><![CDATA[".$timeStamp."]]></timeStamp>\n";
$txt .= $xmlContent;
$txt .= "</values>";

echo $txt;
//print_r($qqRet);

/**
 * 获取好友资料
 *
 * @param object $sdk OpenApiV3 Object
 * @param string $openid openid
 * @param string $openkey openkey
 * @param string $pf 平台
 * @return array 好友资料数组
 */
function get_friends_info(& $sdk, $openid, $openkey, $pf)
{
	$params = array(
		'openid' => $openid,
		'openkey' => $openkey,
		'pf' => $pf,
	);
	
	$script_name = '/v3/relation/get_app_friends';

	return $sdk->api($script_name, $params);
}

function get_multi_info(& $sdk, $openid, $openkey, $pf, $fopenids)
{
	$params = array(
		'openid' => $openid,
		'openkey' => $openkey,
		'pf' => $pf,
		'fopenids' => $fopenids,
	);
	
	$script_name = '/v3/user/get_multi_info';

	return $sdk->api($script_name, $params);
}

?>
